package com.crishne.anwo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crishne.anwo.dao.MenuDao;
import com.crishne.anwo.model.Menu;
import com.crishne.anwo.model.User;
import com.crishne.anwo.service.MenuService;
import com.crishne.anwo.utill.PaginationUtil;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {
	
	 @Autowired
	 private MenuDao menuDao;

	@Override
	public void addMenu(Menu menu) {
		menuDao.addMenu(menu);

	}
	
	//删除信息  
	@Override
    public void deleteMenu(String ids){
		String[] idArray = ids.split(",");
		for(int i=0;i<idArray.length;i++){
			menuDao.deleteMenu(idArray[i]);
		}
	}
    
    //修改信息  
	@Override
    public void updateMenu(Menu menu){
		menuDao.updateMenu(menu);
	}

	@Override
	public List<Menu> getAllMenus() {
		
		return menuDao.getAllMenus();
	}
	
	@Override
	public List<Menu> getHaveUrlMenus() {
		
		return menuDao.getHaveUrlMenus();
	}
	
	 //根据ID查询信息  
	@Override
    public Menu selectMenuById(String id){
		return menuDao.selectMenuById(id);
	}
	
	@Override
	public List<Menu> getMenusByName(String stMenuName){
		return menuDao.getMenusByName(stMenuName);
	}
	
	@Override
	public List<Menu> getMenusByUserId(String stUserId) {
		
		return menuDao.getMenusByUserId(stUserId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PaginationUtil<Menu> searchMenus(PaginationUtil<Menu> pageConditions) {
		//return (PaginationUtil<Menu>) menuDao.searchMenus(pageConditions);
		//当前页
        Integer currentPage = pageConditions.getCurrentPage()==null?1:pageConditions.getCurrentPage();
        //每页数据数
        Integer pageSize = pageConditions.getPageSize()==null?10:pageConditions.getPageSize();
        
    	pageConditions.setCurrentPage(currentPage);
        pageConditions.setPageSize(pageSize);
        //pageConditions.setTotalCount(userList);
        List<Menu> menuList = menuDao.searchMenus(pageConditions);
        int totalCount = menuDao.getTotalResult(pageConditions);
        pageConditions.setDataList(menuList);
        pageConditions.setTotalCount(totalCount);
        if(totalCount == 0){
        	pageConditions.setTotalPage(1);
        }else if(totalCount%pageSize == 0){
        	pageConditions.setTotalPage(totalCount/pageSize);
        }else{
        	pageConditions.setTotalPage(totalCount/pageSize+1);
        }
    	return pageConditions;
	}

}
